package com.my.juc.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author zhupanlin
 * @version 1.0
 * @description: 可重入锁
 * @date 2024/3/5 18:40
 */
public class SyncLockDemo {
    
    public synchronized void add(){
        add();
    }

    public static void main(String[] args) {
        /*SyncLockDemo syncLockDemo = new SyncLockDemo();
        syncLockDemo.add();
        // synchronized
        Object o = new Object();
        new Thread(() -> {
            synchronized (o){
                System.out.println(Thread.currentThread().getName() + " 外层");
                synchronized (o){
                    System.out.println(Thread.currentThread().getName() + " 中层");
                    synchronized (o){
                        System.out.println(Thread.currentThread().getName() + " 内层");
                    }
                }
            }
        }, "t1").start();*/
        
        // Lock
        Lock lock = new ReentrantLock();
        // 创建线程
        new Thread(() -> {
            try {
                lock.lock();
                System.out.println(Thread.currentThread().getName() + " 外层");
                try {
                    lock.lock();
                    System.out.println(Thread.currentThread().getName() + " 内层");

                }finally {
                    //lock.unlock();
                }
            }finally {
                lock.unlock();
            }
        }, "t1").start();
        
        new Thread(() -> {
            lock.lock();
            System.out.println("aaaa");
            lock.unlock();
        }, "aaa").start();
    }
    
}
